﻿using MengXunAppTool.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;

namespace MengXunAppTool.DAL
{
    public class ShenPiDAL
    {
        private Sql3Helper sql3Helper = new Sql3Helper();
        public List<string> GetReleaseUsers()
        {
            var list= sql3Helper.db.Queryable<Sql3Helper.MX_AppUsers>()
                .Where(it => it.workPlace=="YF"||it.workPlace=="PE")
                .Select(it=>it.userName)
                .ToList();
            return list;
        }
        public List<string> GetCheckUsers()
        {
            var list = sql3Helper.db.Queryable<Sql3Helper.MX_AppUsers>()
                .Where(it => it.workPlace == "QE")
                .OrderBy(it => it.userName)
                .Select(it => it.userName)
                .ToList();
            return list;
        }
        public List<string> GetPushUsers(string bm)
        {
            var list = sql3Helper.db.Queryable<Sql3Helper.MX_AppUsers>()
                .WhereIF(bm!="ALL",it => it.workPlace == bm)
                .Select(it => it.userName)
                .ToList();

            return list;
        }
        public bool InitCxUsers()
        {

            var list1 = sql3Helper.db.Queryable<Sql3Helper.MX_AppUsers>().ToList();
            var list2 = sql3Helper.db.Queryable<Sql3Helper.t_DD_UserDetail>().Where(it => it.isStudent == false && it.companyName == "重庆盟讯电子科技有限公司"&&it.workPlace.Contains("制造") && (it.position.Contains("组长")||it.position.Contains("线长"))).OrderBy(it => it.updatetime,SqlSugar.OrderByType.Desc).ToList();
            var users = new List<Sql3Helper.MX_AppUsers>();
            int number = 0;
            foreach (var item in list2)
            {
                if (list1.Count(it => it.userName == item.username) == 0 && int.TryParse(item.jobnumber, out number) && number > 100010)
                {
                    users.Add(new Sql3Helper.MX_AppUsers() { userName = item.username, userid = item.jobnumber, workPlace = "ALL" });
                }
            }
            sql3Helper.db.Insertable(users).ExecuteCommand();
            return true;
        }
        public List<string> GetTemps()
        {
            var list = sql3Helper.db.Queryable<Sql3Helper.MX_AppUsers>()
                .Where(it => it.workPlace != "QE")
                .GroupBy(it => it.workPlace)
                .Select(it => it.workPlace)
                .ToList();
            return list;
        }
        public string GetId(string name)
        {
            return sql3Helper.db.Queryable<Sql3Helper.MX_AppUsers>()
            .Where(it => it.userName == name)
            .First().userid;
        }
        public bool SetCheck(int id)
        {
            return sql3Helper.db.Updateable<Sql3Helper.MX_AppVerRecord>()
                .SetColumns(it => it.CheckTime == sql3Helper.db.GetDate().ToString("yyyy-MM-dd HH:mm:ss"))
                .Where(it => it.id == id)
                .ExecuteCommand() == 1;
        }
    }
}
